package com.nandi.deviceinstall.presenter

import com.nandi.deviceinstall.base.BaseMvpActivity
import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.BaseObjectBean
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.contract.LoginContract
import com.nandi.deviceinstall.model.LoginModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.uber.autodispose.FlowableSubscribeProxy
import org.reactivestreams.Subscription

class LoginPresenter : BasePresenter<LoginContract.View>(),LoginContract.Presenter{
    private val model: LoginContract.Model

    init {
        model = LoginModel()
    }
    override fun login(username: String, password: String) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        //Flowable<BaseObjectBean<LoginBean>>
        model.login(username,password)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<LoginBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<LoginBean>(mView!!) {
                    override fun onNext(t: LoginBean) {
                        mView!!.onSuccess(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        //super.onSubscribe(s)
                        s?.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

}